const path = require('path');
const {
	Service
} = require('dx-router-admin');

module.exports = class Index extends Service {
	constructor(ctx) {
		super(ctx);
	}
	async uploadBase64(base64){
		const cloudPath = 'resume/'+(new Date().getFullYear())+'/'+ (new Date().getTime())+'.png';
		const result = await uniCloud.uploadFile({  
		    cloudPath: cloudPath,  
			cloudPathAsRealPath: true,
		    fileContent: Buffer.from(base64, 'base64')  
		})
		console.info('resultresult:',result)
		return result&&result.fileID;
	}
	company() {
	    //  hasOne('表名称','外键名','主键名')，绑定字段到主数据
		return this.hasOne('human-company', 'company_id', '_id').bindField({
			company_name: 'name',
		});
	}
	async uploadImg(url) {
		const response = await uniCloud.httpclient.request(url, {
			method:'GET',
			responseType:'buffer'
		});
		//buffer.data 有些直接请求图片地址返回的不直接是buffer数组，需要再取一层看情况定
		const cloudPath = 'avatar/'+(new Date().getFullYear())+'/'+path.basename(url);
		let result = await uniCloud.uploadFile({
			cloudPathAsRealPath: true,
			cloudPath: cloudPath,
			fileContent: response.data
		});
        return result&&result.fileID;
	}
};